package ubi.quando.UI;

import java.util.ArrayList;
import java.util.List;

import org.osmdroid.util.GeoPoint;

import ubi.quando.App.Contact;
import ubi.quando.App.ContactWallet;
import ubi.quando.App.EventBase;
import ubi.quando.App.EventType;
import ubi.quando.ServerComm.IServerComm;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

public class UbiApplication extends Application {

	public static final double DEFAULT_LAT = 31.776957;
	public static final double DEFAULT_LONG = 35.196151;

	private ContactWallet mMe;
	private String mTempEventName;

	private List<EventBase> mRunningEvents;
	private EventBase mShowedEvent;
	private Activity mCurrentActivity;
	private IServerComm mServerComm = new IServerComm();
	private NotificationManager mNotificationMgr;
	private String mMail, mPwd;
	
	public static Context CONTEXT; 

	private boolean mInit = false;
	
	@Override
	public void onCreate() {
		CONTEXT = getApplicationContext();
		super.onCreate();
	}

	public List<Contact> getContacts() {
		return mMe.getContacts();
	}

	public List<EventBase> getEvents() {
		return mMe.getEvents();
	}

	public ContactWallet getUser() {
		return mMe;
	}
	
	public EventBase getShowedEvent()
	{
		return mShowedEvent;
	}

	public String getId() {
		return mMe.getId();
	}

	public void setContacts(List<Contact> contacts) {
		mMe.setContacts(contacts);
	}

	public boolean setUser(ContactWallet user) {
		mMe = user;
		if (!mInit) {
			mMe = mServerComm.connect(mMail, mPwd, this);
			if (mMe != null) {
				mInit = true;
				setNotificationManager(new NotificationManager());
			}
		}

		return mMe != null;
	}

	public void setEvents(List<EventBase> events) {
		mMe.setEvents(events);
	}

	public void setId(String id) {
		mMe.setId(id);
	}

	public void addContact(Contact contact) {
		mMe.getContacts().add(contact);
	}

	public void addEvent(EventBase event) {
		mMe.getEvents().add(event);
	}

	public Contact getContactById(String id) {
		List<Contact> contacts = mMe.getContacts();
		for (Contact c : contacts) {
			if (c.getId().equals(id)) {
				return c;
			}
		}
		return null;
	}

	public EventBase getEventById(String id) {
		List<EventBase> events = mMe.getEvents();
		for (EventBase event : events) {
			if (event.getId().equals(id)) {
				return event;
			}
		}
		return null;
	}

	public EventBase getRunningEventById(String id) {
		for (EventBase event : mRunningEvents) {
			if (event.getId().equals(id)) {
				return event;
			}
		}
		return null;
	}

	public boolean Isinitialized() {
		return mInit;
	}

	public String getTempEventName() {
		return mTempEventName;
	}

	public void setTempEventName(String tempEventName) {
		mTempEventName = tempEventName;
	}

	public Activity getCurrentActivity() {
		return mCurrentActivity;
	}

	public void setCurrentActivity(Activity currentActivity) {
		mCurrentActivity = currentActivity;
	}

	public List<EventBase> getRunningEvents() {
		return mRunningEvents;
	}

	public void setmRunningEvents(List<EventBase> runningEvents) {
		mRunningEvents = runningEvents;
	}

	public GeoPoint getMyLocation() {
		return mNotificationMgr.getLocation();
	}

	public void setServerComm(IServerComm serverComm) {
		mServerComm = serverComm;
	}

	public IServerComm getServerComm() {
		return mServerComm;
	}

	public void setNotificationManager(NotificationManager notificationManager) {
		mNotificationMgr = notificationManager;
	}

	public NotificationManager getNotificationManager() {
		return mNotificationMgr;
	}

	public void setMail(String mail) {
		mMail = mail;
	}

	public String getMail() {
		return mMail;
	}

	public void setPwd(String pwd) {
		mPwd = pwd;
	}

	public String getPwd() {
		return mPwd;
	}

	public void HandleEventInvite(String eventId, int eventType, String pickUpLocation, String locName, 
			long pickUpTime) {
		EventType type = EventType.FromInt(eventType);
		EventBase event = type == EventType.MEETING ? mServerComm
				.getMeetingEvent(eventId) : mServerComm.getPickupEvent(eventId);

		Intent run = new Intent(getApplicationContext(), DialogActivity.class);
		Bundle b = new Bundle();

		b.putInt("Type", 2);
		b.putString("EventId", eventId);
		b.putInt("EventType", eventType);
		
		b.putString("PickUpLocation", pickUpLocation);
		b.putString("locName", locName);
		b.putLong("PickUpTime", pickUpTime);
		
		b.putSerializable(EventBase.SerializeName, event);

		run.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

		run.putExtras(b);
		startActivity(run);
	}

	public void HandleUpdateEvent(String eventId, int eventType) {
		EventType type = EventType.FromInt(eventType);
		EventBase event = type == EventType.MEETING ? mServerComm
				.getMeetingEvent(eventId) : mServerComm.getPickupEvent(eventId);

		List<EventBase> events = mMe.getEvents();
		events.remove(getEventById(eventId));
		events.add(event);

		EventBase runningEvent = getRunningEventById(eventId);
		if (runningEvent != null) {
			events.remove(runningEvent);
		}
		if (event.isStarted()) {
			events.add(event);
		}
	}

	public void HandleEventStarted(String eventId) {
		List<EventBase> events = mMe.getEvents();
		for (EventBase event : events) {
			if (event.getId().equals(eventId)) {
				event.setStarted(true);
				if (mRunningEvents == null) {
					mRunningEvents = new ArrayList<EventBase>();
				}
				mRunningEvents.add(event);
				mShowedEvent = event;
				mNotificationMgr.notifyEventStarted(event);

				Intent run = new Intent(this, EventRunningActivity.class);
				Bundle b = new Bundle();
				b.putSerializable(EventBase.SerializeName, mShowedEvent);
				
				run.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

				run.putExtras(b);
				startActivity(run);
			}
		}
	}

	public void HandleContactAdded(String contactId) {
		// need to add to HandleContactAdded, String contactName!
		Contact newContact = mMe.addContact(mServerComm.getContact(contactId));
		Intent run = new Intent(getApplicationContext(), DialogActivity.class);
		Bundle b = new Bundle();

		b.putInt("Type", 3);
		b.putString("Notification", newContact.getName()+" Accepted Your Friend Request." +
				"\nYou Are Now Friends.");

		run.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

		run.putExtras(b);
		startActivity(run);
	}

	public void HandleOfferPickup(String id) {
		// TODO Auto-generated method stub

	}

	public void HandlePickupConfirmed(String eventId) {
		// TODO Auto-generated method stub

	}

	public void HandleUpdateLocation(String clientId, String location) {
		if (mCurrentActivity instanceof EventRunningActivity) {
			((EventRunningActivity) mCurrentActivity)
					.UpdateParticipantsLocation(clientId, location);
		}
		mNotificationMgr.onClientLocationChanged();
	}

	public void HandleFriendRequest(String name, String id) {
		Intent run = new Intent(getApplicationContext(), DialogActivity.class);
		Bundle b = new Bundle();

		b.putInt("Type", 1);
		b.putString("Id", id);
		b.putString("Name", name);

		run.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

		run.putExtras(b);
		startActivity(run);
	}
}
